package com.android;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseManager {
	private static final String String = null;

	Context context;
	 
	private SQLiteDatabase db;

	private final String DB_NAME = "database_puzzle";
	private final int DB_VERSION = 1;

	private final String TABLE_NAME = "database_table";
	private final String TABLE_ROW_ID = "id";
	private final String TABLE_ROW_ONE = "table_row_one";
	private final String TABLE_ROW_TWO = "table_row_two";
	
	public DataBaseManager(Context context) {
		
		this.context = context;
		
		CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
		this.db = helper.getWritableDatabase();
	}
	
	public void addRow(String rowStringOne, String rowStringTwo){
		ContentValues values = new ContentValues();
		values.put(TABLE_ROW_ONE, rowStringOne);
		values.put(TABLE_ROW_TWO, rowStringTwo);
 
		try{
			db.insert(TABLE_NAME, null, values);
		}catch(Exception e){
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
		}
	}
	
	public void deleteRow(long rowID){
		try {db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);}
		catch (Exception e)
		{
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
		}
	}
	
	public void updateRow(long rowID, String rowStringOne, String rowStringTwo){
		ContentValues values = new ContentValues();
		values.put(TABLE_ROW_ONE, rowStringOne);
		values.put(TABLE_ROW_TWO, rowStringTwo);
 
		try {db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);}
		catch (Exception e)
		{
			Log.e("DB Error", e.toString());
			e.printStackTrace();
		}
	}
	public ArrayList<Object> getRowAsArray(){
		ArrayList<Object> rowArray = new ArrayList<Object>();
		Cursor cursor;
 
		try
		{
			String orerby = "order by TABLE_ROW_ONE desc";
			cursor = db.query
			(
					TABLE_NAME,
					new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO },
					null,null, null, null,orerby 
			);
 
			cursor.moveToFirst();
 
			if (!cursor.isAfterLast())
			{
				do
				{
					rowArray.add(cursor.getLong(0));
					rowArray.add(cursor.getString(1));
					rowArray.add(cursor.getString(2));
				}
				while (cursor.moveToNext());
			}
 
			cursor.close();
		}
		catch (SQLException e) 
		{
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
		}
 
		return rowArray;
	}
	public ArrayList<ArrayList<Object>> getAllRowsAsArrays(){
		
		ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
		 
		Cursor cursor;
 
		try
		{
			String orerby = ""+TABLE_ROW_ONE+" ASC , "+TABLE_ROW_TWO+" ASC";
			cursor = db.query(
					TABLE_NAME,
					new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
					null, null, null, null, orerby
			);
			
			cursor.moveToFirst();
 
			if (!cursor.isAfterLast())
			{
				do
				{
					ArrayList<Object> dataList = new ArrayList<Object>();
 
					dataList.add(cursor.getLong(0));
					dataList.add(cursor.getString(1));
					dataList.add(cursor.getString(2));
 
					dataArrays.add(dataList);
				}
				while (cursor.moveToNext());
			}
		}
		catch (SQLException e)
		{
			Log.e("DB Error", e.toString());
			e.printStackTrace();
		}
 
		return dataArrays;
		
	}
	private class CustomSQLiteOpenHelper extends SQLiteOpenHelper{

		public CustomSQLiteOpenHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String newTableQueryString = "create table " +
			TABLE_NAME +
			" (" +
			TABLE_ROW_ID + " integer primary key autoincrement not null," +
			TABLE_ROW_ONE + " text," +
			TABLE_ROW_TWO + " text" +
			");";
			db.execSQL(newTableQueryString);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

			// NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
			// OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
		}
		
	}
	
}
